// app
#include "robot_def.h"
#include "robot_cmd.h"
// module
#include "remote_control.h"
#include "ins_task.h"
#include "master_process.h"
#include "message_center.h"
#include "general_def.h"
#include "dji_motor.h"
#include "bmi088.h"
#include "user_lib.h"
#include "can_comm.h"
// bsp
#include "bsp_dwt.h"
#include "bsp_log.h"
#include "string.h"

static Publisher_t *chassis_cmd_pub;             // 底盘控制消息发布者
static Subscriber_t *chassis_feed_sub;           // 底盘反馈信息订阅者
static Chassis_Ctrl_Cmd_s chassis_cmd_send;      // 发送给底盘应用的信息,包括控制信息和UI绘制相关
static Chassis_Upload_Data_s chassis_fetch_data; // 从底盘应用接收的反馈信息信息,底盘功率枪口热量与底盘运动状态等
static Chassis_Can_Comm chassis_can_send;

static RC_ctrl_t *rc_data;              // 遥控器数据,初始化时返回
static Vision_Recv_s *vision_recv_data; // 视觉接收数据指针,初始化时返回
static Vision_Send_s vision_send_data;  // 视觉发送数据

static Publisher_t *gimbal_cmd_pub;            // 云台控制消息发布者
static Subscriber_t *gimbal_feed_sub;          // 云台反馈信息订阅者
static Gimbal_Ctrl_Cmd_s gimbal_cmd_send;      // 传递给云台的控制信息
static Gimbal_Upload_Data_s gimbal_fetch_data; // 从云台获取的反馈信息

static Publisher_t *shoot_cmd_pub;           // 发射控制消息发布者
static Subscriber_t *shoot_feed_sub;         // 发射反馈信息订阅者
static Shoot_Ctrl_Cmd_s shoot_cmd_send;      // 传递给发射的控制信息
static Shoot_Upload_Data_s shoot_fetch_data; // 从发射获取的反馈信息

static Robot_Status_e robot_state; // 机器人整体工作状态

static CANCommInstance *cmd_can_comm; // 底盘CAN通信实例

static chassis_direction_e chassis_direction; // 底盘方向

void RobotCMDInit()
{
    //rc_data = RemoteControlInit(&huart3);   // 修改为对应串口,注意如果是自研板dbus协议串口需选用添加了反相器的那个
    //vision_recv_data = VisionInit(&huart1); // 视觉通信串口

    // gimbal_cmd_pub = PubRegister("gimbal_cmd", sizeof(Gimbal_Ctrl_Cmd_s));
    // gimbal_feed_sub = SubRegister("gimbal_feed", sizeof(Gimbal_Upload_Data_s));
    // shoot_cmd_pub = PubRegister("shoot_cmd", sizeof(Shoot_Ctrl_Cmd_s));
    // shoot_feed_sub = SubRegister("shoot_feed", sizeof(Shoot_Upload_Data_s));

    // CANComm_Init_Config_s comm_conf = {
    //     .can_config = {
    //         .can_handle = &hcan1,
    //         .tx_id = 0x312,
    //         .rx_id = 0x311,
    //     },
    //     .recv_data_len = sizeof(Chassis_Upload_Data_s),
    //     .send_data_len = sizeof(Chassis_Ctrl_Cmd_s),
    // };
    // cmd_can_comm = CANCommInit(&comm_conf);

    gimbal_cmd_send.pitch = 0;
    gimbal_cmd_send.yaw = 0;
    gimbal_cmd_send.gimbal_mode = GIMBAL_ZERO_FORCE;
    chassis_cmd_send.chassis_mode = CHASSIS_ZERO_FORCE;
    shoot_cmd_send.shoot_mode = SHOOT_OFF;
    shoot_cmd_send.load_mode = LOAD_STOP;
    shoot_cmd_send.friction_mode = FRICTION_OFF;
    chassis_direction = CHASSIS_ALIGN;

    robot_state = ROBOT_STOP; // 启动时机器人进入工作模式,后续加入所有应用初始化完成之后再进入
}

/**
 * @brief 根据gimbal app传回的当前电机角度计算和零位的误差
 *        单圈绝对角度的范围是0~360,说明文档中有图示
 *
 */
static void CalcOffsetAngle()
{
    // @todo:相差一整圈时会出问题，待修复
    // 别名angle提高可读性,不然太长了不好看,虽然基本不会动这个函数
    uint16_t yaw_chassis_align_ecd;
    if (chassis_direction == CHASSIS_ALIGN)
    {
        yaw_chassis_align_ecd = 2716;
    }
    else if (chassis_direction == CHASSIS_SIDLE)
    {
        yaw_chassis_align_ecd = 765;
    }

    static float angle, yaw_align_angle;
    yaw_align_angle = yaw_chassis_align_ecd * ECD_ANGLE_COEF_DJI; // 从底盘获取的yaw电机对齐角度
    angle = gimbal_fetch_data.yaw_motor_single_round_angle;       // 从云台获取的当前yaw电机单圈角度

    if (yaw_chassis_align_ecd > 4096) // 如果大于180度
    {
        if (angle > yaw_align_angle)
            chassis_cmd_send.offset_angle = angle - yaw_align_angle;
        else if (angle <= yaw_align_angle && angle >= yaw_align_angle - 180.0f)
            chassis_cmd_send.offset_angle = angle - yaw_align_angle;
        else
            chassis_cmd_send.offset_angle = angle - yaw_align_angle + 360.0f;
    }
    else
    { // 小于180度
        if (angle > yaw_align_angle && angle <= 180.0f + yaw_align_angle)
            chassis_cmd_send.offset_angle = angle - yaw_align_angle;
        else if (angle > 180.0f + yaw_align_angle)
            chassis_cmd_send.offset_angle = angle - yaw_align_angle - 360.0f;
        else
            chassis_cmd_send.offset_angle = angle - yaw_align_angle;
    }
}

/**
 * @brief 控制输入为遥控器(调试时)的模式和控制量设置
 *
 */
int aaaa;
static void RemoteControlSet()
{
    memset(&rc_data->key_count, 0, sizeof(rc_data->key_count));
    gimbal_cmd_send.gimbal_mode = GIMBAL_GYRO_MODE;

    // 云台参数,确定云台控制数据
    gimbal_cmd_send.yaw -= 0.001f * (float)rc_data[TEMP].rc.rocker_l_;
    gimbal_cmd_send.pitch -= 0.0006f * (float)rc_data[TEMP].rc.rocker_l1;
    chassis_cmd_send.rotate_w = 0;
    chassis_cmd_send.delta_leglen = 0.0f;
    // 摇杆控制的软件限位
    gimbal_cmd_send.pitch = float_constrain(gimbal_cmd_send.pitch, PITCH_MIN_ANGLE, PITCH_MAX_ANGLE);

    if (switch_is_down(rc_data[TEMP].rc.switch_left)) // 左侧开关状态为[下],视觉模式
    {
        gimbal_cmd_send.yaw = (vision_recv_data->yaw == 0 ? gimbal_cmd_send.yaw : vision_recv_data->yaw);
        gimbal_cmd_send.pitch = (vision_recv_data->pitch == 0 ? gimbal_cmd_send.pitch : vision_recv_data->pitch);
    }

    // 底盘参数
    if (abs(rc_data[TEMP].rc.rocker_r1) > 500)
    {
        chassis_direction = CHASSIS_ALIGN;
    }
    else if (abs(rc_data[TEMP].rc.rocker_r_) > 500)
    {
        chassis_direction = CHASSIS_SIDLE;
    }

    // 右侧开关状态为[下],底盘旋转
    if (switch_is_down(rc_data[TEMP].rc.switch_right))
    {
        chassis_cmd_send.chassis_mode = CHASSIS_FOLLOW_GIMBAL_YAW;

        if (abs(rc_data[TEMP].rc.rocker_r_) > 300)
        {
            chassis_cmd_send.chassis_mode = CHASSIS_ROTATE;
            chassis_cmd_send.rotate_w = 2;
        }
        if (abs(rc_data[TEMP].rc.rocker_r1) > 300)
        {
            chassis_cmd_send.chassis_mode = CHASSIS_ROTATE_REVERSE;
            chassis_cmd_send.rotate_w = -2;
        }
        chassis_cmd_send.vx = 0;
    }
    else
    {
        chassis_cmd_send.chassis_mode = CHASSIS_FOLLOW_GIMBAL_YAW;
        chassis_cmd_send.vx = 0.003f * (-(float)rc_data[TEMP].rc.rocker_r_ + (float)rc_data[TEMP].rc.rocker_r1);
    }

    // 左侧开关状态为[中]，腿长控制
    if (switch_is_mid(rc_data[TEMP].rc.switch_left))
    {
        chassis_cmd_send.delta_leglen = -(abs(rc_data[TEMP].rc.dial) > 100 ? (int8_t)(rc_data[TEMP].rc.dial / 6) : 0);
    }

    // 摩擦轮控制,拨轮向上打为负,向下为正
    if (switch_is_down(rc_data[TEMP].rc.switch_left))
    {
        if (rc_data[TEMP].rc.dial < -100) // 向上超过100,打开摩擦轮
            shoot_cmd_send.friction_mode = FRICTION_ON;
        else
            shoot_cmd_send.friction_mode = FRICTION_OFF;

        // 拨弹控制,遥控器固定为一种拨弹模式,可自行选择
        if (rc_data[TEMP].rc.dial < -400)
            shoot_cmd_send.load_mode = LOAD_BURSTFIRE;
        else
            shoot_cmd_send.load_mode = LOAD_STOP;
    }

    // 发射参数
    shoot_cmd_send.bullet_speed = 30;
    shoot_cmd_send.shoot_rate = 15;
}

/**
 * @brief 输入为键鼠时模式和控制量设置
 *
 */
static void MouseKeySet()
{
    gimbal_cmd_send.gimbal_mode = GIMBAL_GYRO_MODE; //云台默认为陀螺仪模式
    shoot_cmd_send.shoot_mode = SHOOT_ON;
    gimbal_cmd_send.yaw -= (float)(rc_data[TEMP].mouse.x + rc_data[LAST].mouse.x) / 660.0f * 4.0f; // 系数待测
    gimbal_cmd_send.pitch += (float)(rc_data[TEMP].mouse.y + rc_data[TEMP].mouse.y) / 660.0f * 4.0f;

    gimbal_cmd_send.pitch = float_constrain(gimbal_cmd_send.pitch, PITCH_MIN_ANGLE, PITCH_MAX_ANGLE); // 限制pitch角度

    if (chassis_cmd_send.chassis_mode == CHASSIS_FOLLOW_GIMBAL_YAW) // 底盘速度输入 默认无前进轮速
    {
        chassis_cmd_send.vx = BALANCE_MAX_SPEED * (float)(rc_data[TEMP].key[KEY_PRESS].w - rc_data[TEMP].key[KEY_PRESS].s - rc_data[TEMP].key[KEY_PRESS].a + rc_data[TEMP].key[KEY_PRESS].d);
    }
    else if (chassis_cmd_send.chassis_mode == CHASSIS_ROTATE)
    {
        chassis_cmd_send.vx = 0;
    }

    chassis_cmd_send.delta_leglen = (rc_data[TEMP].key[KEY_PRESS].e - rc_data[TEMP].key[KEY_PRESS].c) * 10; // E C控制腿长

    //wsad控制底盘方向 但会有额外速度输入 不使用
    if (rc_data[TEMP].key[KEY_PRESS].w || rc_data[TEMP].key[KEY_PRESS].s) 
    {
        chassis_direction = CHASSIS_ALIGN;
    }
    else if (rc_data[TEMP].key[KEY_PRESS].a || rc_data[TEMP].key[KEY_PRESS].d)
    {
        chassis_direction = CHASSIS_SIDLE;
    }
    
    // switch(rc_data[TEMP].key_count[KEY_PRESS][Key_X] % 2) // x键旋转
    // {
    //     case 0:
    //         chassis_direction = CHASSIS_ALIGN;
    //         break;
    //     case 1:
    //         chassis_direction = CHASSIS_SIDLE;
    //         break;
    // }

    switch (rc_data[TEMP].key_count[KEY_PRESS][Key_B] % 2) // B键开启底盘跟随云台 仅重置底盘后开启
    {
    case 0:
        break;
    case 1:
        chassis_cmd_send.chassis_mode = CHASSIS_FOLLOW_GIMBAL_YAW;
        break;
    }

    switch (rc_data[TEMP].key_count[KEY_PRESS][Key_F] % 2) // F 摩擦轮
    {
    case 0:
        shoot_cmd_send.friction_mode = FRICTION_OFF;
        break;
    default:
        shoot_cmd_send.friction_mode = FRICTION_ON;
        break;
    }

    if (shoot_cmd_send.friction_mode == FRICTION_ON)
    {
        switch (rc_data[TEMP].key_count[KEY_PRESS][Key_V] % 2) // V 拨弹模式切换
        {
        case 0:
            shoot_cmd_send.load_mode = LOAD_BURSTFIRE;
            break;
        case 1:
            shoot_cmd_send.load_mode = LOAD_1_BULLET;
            break;
        }
        if (rc_data[TEMP].mouse.press_l) // 鼠标左键发射
        {
        }
        else
            shoot_cmd_send.load_mode = LOAD_STOP;
    }
    else
        shoot_cmd_send.load_mode = LOAD_STOP;

    switch (rc_data[TEMP].key[KEY_PRESS].z) // Z键刷新UI
    {
    case 0:
        chassis_cmd_send.ui_mode = UI_KEEP;
        break;
    default:
        chassis_cmd_send.ui_mode = UI_REFRESH;
        break;
    }

    if (rc_data[TEMP].mouse.press_r) // 鼠标右键视觉模式
    {
        gimbal_cmd_send.yaw = (vision_recv_data->yaw == 0 ? gimbal_cmd_send.yaw : vision_recv_data->yaw);
        gimbal_cmd_send.pitch = (vision_recv_data->pitch == 0 ? gimbal_cmd_send.pitch : vision_recv_data->pitch);
    }
    if (rc_data[TEMP].key[KEY_PRESS].q) // Q键底盘旋转
    {
        chassis_cmd_send.chassis_mode = CHASSIS_ROTATE;
        chassis_cmd_send.rotate_w = 6;
    }
    else{
        chassis_cmd_send.chassis_mode = CHASSIS_FOLLOW_GIMBAL_YAW;
    }

    if (rc_data[TEMP].key[KEY_PRESS].r) // R键底盘复位
    {
        chassis_cmd_send.chassis_mode = CHASSIS_RESET;
    }
}

/**
 * @brief  紧急停止,包括遥控器左上侧拨轮打满/重要模块离线/双板通信失效等
 *         停止的阈值'300'待修改成合适的值,或改为开关控制.
 *
 * @todo   后续修改为遥控器离线则电机停止(关闭遥控器急停),通过给遥控器模块添加daemon实现
 *
 */
static void EmergencyHandler()
{
    if (switch_is_down(rc_data[TEMP].rc.switch_left) || switch_is_mid(rc_data[TEMP].rc.switch_left)) // 遥控器左侧开关状态为[下],遥控器控制
    {
        if ((rc_data[TEMP].rc.dial > 300 && switch_is_down(rc_data[TEMP].rc.switch_left)) || robot_state == ROBOT_STOP) // 还需添加重要应用和模块离线的判断
        {
            robot_state = ROBOT_STOP;
            gimbal_cmd_send.gimbal_mode = GIMBAL_ZERO_FORCE;
            chassis_cmd_send.chassis_mode = CHASSIS_ZERO_FORCE;
            shoot_cmd_send.shoot_mode = SHOOT_OFF;
            shoot_cmd_send.friction_mode = FRICTION_OFF;
            shoot_cmd_send.load_mode = LOAD_STOP;

            gimbal_cmd_send.yaw = gimbal_fetch_data.gimbal_imu_data.YawTotalAngle; // 急停时设定值保持与实际值同步，避免恢复时疯转
            gimbal_cmd_send.pitch = gimbal_fetch_data.gimbal_imu_data.Pitch;
        }
        // 遥控器右侧开关为[上],恢复正常运行
        if (switch_is_up(rc_data[TEMP].rc.switch_right))
        {
            robot_state = ROBOT_READY;
            shoot_cmd_send.shoot_mode = SHOOT_ON;
            gimbal_cmd_send.gimbal_mode = GIMBAL_GYRO_MODE;
        }
    }
    else if (switch_is_up(rc_data[TEMP].rc.switch_left)) // 遥控器左侧开关状态为[上],键盘控制
    {
        switch (rc_data[TEMP].key_count[KEY_PRESS_WITH_CTRL][Key_C] % 2) // ctrl+c 进入急停
        {
        case 0:
            // 没按保持源状态不变
            // 云台会先自动恢复
            break;

        default:
            robot_state = ROBOT_STOP;
            gimbal_cmd_send.gimbal_mode = GIMBAL_ZERO_FORCE;
            chassis_cmd_send.chassis_mode = CHASSIS_ZERO_FORCE;
            shoot_cmd_send.shoot_mode = SHOOT_OFF;
            shoot_cmd_send.friction_mode = FRICTION_OFF;
            shoot_cmd_send.load_mode = LOAD_STOP;

            gimbal_cmd_send.yaw = gimbal_fetch_data.gimbal_imu_data.YawTotalAngle; // 急停时设定值保持与实际值同步，避免恢复时疯转
            gimbal_cmd_send.pitch = gimbal_fetch_data.gimbal_imu_data.Pitch;
            break;
        }
    }
}
static uint8_t can_comm_cnt = 0;
/* 机器人核心控制任务,200Hz频率运行(必须高于视觉发送频率) */
void RobotCMDTask()
{
    chassis_fetch_data = *(Chassis_Upload_Data_s *)CANCommGet(cmd_can_comm);

    // SubGetMessage(shoot_feed_sub, &shoot_fetch_data);
    // SubGetMessage(gimbal_feed_sub, &gimbal_fetch_data);

    // 根据gimbal的反馈值计算云台和底盘正方向的夹角,不需要传参,通过static私有变量完成
   // CalcOffsetAngle();
    // 根据遥控器左侧开关,确定当前使用的控制模式为遥控器调试还是键鼠
    if (switch_is_down(rc_data[TEMP].rc.switch_left) || switch_is_mid(rc_data[TEMP].rc.switch_left)) // 遥控器左侧开关状态为[下],遥控器控制
        RemoteControlSet();
    if (switch_is_up(rc_data[TEMP].rc.switch_left)) // 遥控器左侧开关状态为[上],键盘控制
        MouseKeySet();

    EmergencyHandler(); // 处理模块离线和遥控器急停等紧急情况

    chassis_cmd_send.direction = chassis_direction;
    // UI数据
    chassis_cmd_send.friction_mode = shoot_cmd_send.friction_mode;
    chassis_cmd_send.loader_mode = shoot_cmd_send.load_mode;
    // 临时写法,chassis_can_send只在以下行代码中使用,后续修改
    // chassis_can_send.vx = chassis_cmd_send.vx;
    // chassis_can_send.offset_angle = chassis_cmd_send.offset_angle;
    // chassis_can_send.delta_leglen = chassis_cmd_send.delta_leglen;
    // chassis_can_send.can_comm.chassis_direction_e = chassis_cmd_send.direction;
    // chassis_can_send.can_comm.chassis_mode = chassis_cmd_send.chassis_mode;
    // chassis_can_send.can_comm.friction_mode_e = chassis_cmd_send.friction_mode;
    // chassis_can_send.can_comm.loader_mode = chassis_cmd_send.loader_mode;
    // chassis_can_send.can_comm.ui_mode = chassis_cmd_send.ui_mode;
    // chassis_can_send.can_comm.vision_mode = chassis_cmd_send.vision_mode;

    can_comm_cnt++;
    if (can_comm_cnt % 2 == 0)
    {
        CANCommSend(cmd_can_comm, (void *)&chassis_cmd_send);
        can_comm_cnt = 0;
    }
    PubPushMessage(shoot_cmd_pub, (void *)&shoot_cmd_send);
    PubPushMessage(gimbal_cmd_pub, (void *)&gimbal_cmd_send);
}
